# -*- coding: utf-8 -*-
from datetime import datetime
from django.db import models
from common.log import logger

class HostStatusDataManager(models.Manager):
    def save_data(self, data):
        """
        保存执行结果数据
        """
        try:
            HostStatusData.objects.create(
                ip=data[4],
                scpu=data[3],
                smem=data[1],
                sdisk=data[2],
                createtime=datetime.strptime(data[0], "%Y-%m-%d %H:%M:%S")#查询时间
            )
            result = {'result': True, 'message': u"保存成功"}
        except Exception, e:
            logger.error(u"save_data %s" % e)
            result = {'result': False, 'message': u"保存失败, %s" % e}
        return result

class HostStatusData(models.Model):
    """
    存储查询的主机状态数据
    """
    ip = models.CharField('ip', max_length=64, blank=True, null=True)
    scpu = models.CharField('scpu', max_length=64)
    smem = models.CharField('smem', max_length=64)
    sdisk = models.CharField('sdisk', max_length=64)
    runtime =  models.DateTimeField(u"查询时间")
    objects = HostStatusDataManager()

    
    def __unicode__(self):
        return self.ip

    class Meta:
        verbose_name = u"主机状态数据"
        verbose_name_plural = u"主机状态数据"


class OperateHistoryDataManager(models.Manager):
    def save_data(self, data):
        """
        保存执行结果数据
        """
        try:
            OperateHistoryData.objects.create(
                ip=data[0],
                user=data[2],
                createtime=datetime.strptime(data[1], "%Y-%m-%d %H:%M:%S")#查询时间
            )
            result = {'result': True, 'message': u"保存成功"}
        except Exception, e:
            logger.error(u"save_data %s" % e)
            result = {'result': False, 'message': u"保存失败, %s" % e}
        return result

class OperateHistoryData(models.Model):
    """
    存储操作历史数据
    """
    ip = models.CharField('ip', max_length=64, blank=True, null=True)
    user = models.CharField('user', max_length=64)
    operatetime =  models.DateTimeField(u"操作时间")
    objects = OperateHistoryDataManager()

    
    def __unicode__(self):
        return self.ip

    class Meta:
        verbose_name = u"操作历史数据"
        verbose_name_plural = u"操作历史数据"

